package com.yqg.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.manage.common.interceptor.SysLog;
import com.yqg.manage.entity.RoleMenu;
import com.yqg.manage.entity.UnitMenu;
import com.yqg.manage.entity.User;
import com.yqg.manage.mapper.LawsMapper;
import com.yqg.manage.mapper.UnitMapper;
import com.yqg.manage.service.IRoleMenuService;
import com.yqg.manage.service.IUnitMenuService;
import com.yqg.manage.service.IUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yqg.manage.common.Result;

import com.yqg.manage.service.IUnitService;
import com.yqg.manage.entity.Unit;

import org.springframework.web.bind.annotation.RestController;

/**
 * UnitController 前端控制器
 *
 * @author zxf
 * @since 2025-09-16
 */
@RestController
@RequestMapping("/unit")
public class UnitController {

    @Resource
    private IUnitService unitService;
    @Resource
    private UnitMapper unitMapper;
    @Resource
    private IUnitMenuService unitMenuService;
    @Resource
    private IUserService userService;
    @Resource
    private IRoleMenuService roleMenuService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    @SysLog("新增或者更新内设部门")
    public Result save(@RequestBody Unit unit) {
        boolean isSuccess = unitService.saveOrUpdate(unit);
        if (isSuccess) {
            isSuccess = unitMenuService.saveOrUpdateUnitMenu(unit.getMenuIds(), unit.getId());
        }
        return Result.success(isSuccess);
    }

    /**
     * 删除前验证该单位是否有用户
     */
    @GetMapping("deleteBeforeVerify")
    public Result deleteBeforeVerify(@RequestParam Long id) {
        Integer b = unitMapper.deleteBeforeVerify(id);
        return Result.success(b);
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    @SysLog("删除内设部门")
    public Result delete(@PathVariable Integer id) {
        unitMapper.deleteUnit(id);
        return Result.success();
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<Unit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", "0");
        List<Unit> list = unitService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 副书记获取的单位
     */
    @GetMapping("/deputyDirectorFindAll")
    public Result deputyDirectorFindAll(@RequestParam Long userId) {
        List<Unit> list = unitMapper.deputyDirectorFindAll(userId);
        return Result.success(list);
    }

    /**
     * 查询过滤掉管委会的单位
     * @param userId 副主任ID，不存在表示查所有，存在表示查副主任负责的单位
     */
    @GetMapping("/filterAfterAllUnit")
    public Result filterAfterAllUnit(@RequestParam(required = false) Long userId) {
        List<Unit> list = unitMapper.filterAfterAllUnit(userId);
        return Result.success(list);
    }


    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(unitService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String search) {

        LambdaQueryWrapper<Unit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Unit::getIsDelete, "0")
                .orderByAsc(Unit::getId);
        if (search != null) {
            queryWrapper.like(Unit::getName, search);
        }
        return Result.success(unitService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

    /**
     * 副主任查询自己负责的单位
     */
    @GetMapping("/deputyDirectorFindPage")
    public Result deputyDirectorFindPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String search,
            @RequestParam Long userId) {
        pageNum = (pageNum - 1) * pageSize;
        List<Unit> unitList = unitMapper.deputyDirectorFindPage(pageNum, pageSize, search,userId);
        Integer count = unitMapper.deputyDirectorFindPageCount(search,userId);
        Map<String, Object> map = new HashMap<>();
        map.put("records", unitList);
        map.put("total", count);
        return Result.success(map);
    }

    /**
     * 获取所有单位简称
     */
    @GetMapping("/getAllUnitDocumentNumber")
    public Result getAllUnitDocumentNumber() {
        return Result.success(unitService.getAllUnitDocumentNumber());
    }

}

